/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.ui.client.actions;

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.opu.pc.services.PublisherService;
import org.opu.pc.services.beans.AbstractBean;
import org.opu.pc.services.beans.PublisherBean;
import org.opu.pc.ui.client.MainView;
import org.opu.pc.ui.util.Options;
import org.opu.pc.ui.util.Options.Option;

/**
 *
 * @author yura
 */
public class MergePublishers extends AbstractAction
		implements PropertyChangeListener {

    /*
     *  We cannot perform this action more than one time.
     */
	
	private static final long serialVersionUID = 6200284074829131150L;
	
	private PublisherService publisherService;
    private boolean canBeAnable = false;

    public void setPublisherService(PublisherService publisherService) {
		this.publisherService = publisherService;
	}

    @Override
    public String getName() {
        return "MergePublishers";
    }

	@SuppressWarnings("unchecked")
	@Override
    public void runAction(ActionEvent e) throws Exception {
        List<? extends AbstractBean> selected = mainView.getSelectedBeans();
        if (selected == null || selected.size() < 2){
            Options.error(getSafety("MergePublishers.err1", "You can perform " +
            		"this action only if you select more then one publisher"));
            return;
        }

        AbstractBean get = (AbstractBean) selected.get(0);
        if (!(get instanceof PublisherBean)){
            Options.error(getSafety("MergePublishers.err2", "You have to " +
            		"select publisher (not publication)"));
            return;
        }

        Option choise = Options.choise(getSafety("MergePublishers.msg1", 
        		"Select real publisher"), selected);
        if (choise.code == Options.CH_OK_OPTION){
        	selected.remove(choise.source);
            publisherService.merge((PublisherBean)choise.source, (List<PublisherBean>) selected);
        }
    }
    

	@Override
	public void setMainView(MainView mainView) {
		super.setMainView(mainView);
		mainView.addChangeListener(this);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (MainView.SELECTED_BEANS.equals(evt.getPropertyName())){
			List list = (List) evt.getNewValue();
			setEnabled((list != null && list.size() > 1) && canBeAnable);
		} else if (MainView.CONTENT_TYPE.equals(evt.getPropertyName())){
			canBeAnable = (PublisherBean.class.equals(evt.getNewValue()));
		}
	}

}
